package fichier;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;


public class Box {
	private int ID;
	private String nom;
	private static final String table = "box";
    public static final ArrayList < Box > boxList = new ArrayList < Box >();
	
	
	
	public int getID() {
		return ID;
	}
	public String getNom() {
		return nom;
	}
	public void setID(int iD) {
		ID = iD;
	}
	public void setNom(String nom) {
		this.nom = nom;
	}
	public Box(int iD){
		super();
		SelectData.REQUETE = "select * " +
			"from box " +
			"where id_box = " + iD;
		ResultSet infoBox = SelectData.startSelect();
		
		try {
			if(infoBox.next());{
				this.setNom(infoBox.getString("nom_box"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public Box(String nom, int inutile){
		super();
		SelectData.REQUETE = "select * " +
			"from box " +
			"where nom_box = '" + nom + "'";
		ResultSet infoBox = SelectData.startSelect();
		
		try {
			if(infoBox.next());{
				this.setID(Integer.parseInt(infoBox.getString("id_box")));
				this.setNom(infoBox.getString("nom_box"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public Box(int iD, String nom) {
		super();
		this.ID = iD;
		this.nom = SQLadaptString.adapt(nom);
	}
	public Box(String nom) {
		super();
		this.nom = SQLadaptString.adapt(nom);
		this.ajouter();
		Box.boxList.add(this);
	}
	
	private void ajouter(){
		SelectData.REQUETE  = "INSERT INTO "+ table +" VALUES (null,'"+ this.getNom() +"')";
		System.out.println(SelectData.REQUETE);
		SelectData.startUpdate();
		SelectData.REQUETE = "SELECT MAX(id_box) as 'test' FROM "+ table;
		ResultSet result = SelectData.startSelect();
		String s;
		try {
			while(result.next()){
				s = result.getString("test");
				int lastID = Integer.parseInt( s );
				this.setID(lastID);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void supprimer(){
		SelectData.REQUETE = "DELETE FROM "+ table +" WHERE id_box ="+this.getID();
		System.out.println(SelectData.REQUETE);
		SelectData.startUpdate();
	}
	
	
	

}
